#include "StdAfx.h"
#include "OrganFactory.h"
#include <vector>
#include <cmath>


COrganFactory::COrganFactory(void)
{
	m_amps = 1.0;
	m_drawbarNum = 888000000;
	m_drawbars.resize(9,0.0);

	m_LeslieFrequency = 0.0;
	m_LeslieStartFrequency = 0.0;
	m_LeslieEndFrequency = 0.0;
	m_LeslieRadius = 0.0;
	m_LeslieAttackTime = 0.0;
	m_LeslieReleaseTime = 0.0;
	m_changeleslie = false;
}


COrganFactory::~COrganFactory(void)
{
}
COrganInstrument *COrganFactory::CreateInstrument()
{
    COrganInstrument *instrument = new COrganInstrument();

    instrument->SetAmplitude(m_amps);
	instrument->SetDrawbars(m_drawbars);

	instrument->SetVibratoFreq(m_VibratoFrequency);
	instrument->SetVibratoMag(m_VibratoMagnitude);
	
	instrument->SetLeslieFreq(m_LeslieFrequency);
	instrument->SetLeslieStartFreq(m_LeslieStartFrequency);
	instrument->SetLeslieEndFreq(m_LeslieEndFrequency);
	instrument->SetLeslieAttackTime(m_LeslieAttackTime);
	instrument->SetLeslieReleaseTime(m_LeslieReleaseTime);
	instrument->SetLeslieRadius(m_LeslieRadius);
	instrument->SetLeslieChanged(m_changeleslie);

    return instrument;
}


void COrganFactory::SetNote(CNote *note)
{
    // Get a list of all attribute nodes and the
    // length of that list
    CComPtr<IXMLDOMNamedNodeMap> attributes;
    note->Node()->get_attributes(&attributes);
    long len;
    attributes->get_length(&len);

	double m_Leslie = m_LeslieFrequency;
    // Loop over the list of attributes
    for(int i=0;  i<len;  i++)
    {
        // Get attribute i
        CComPtr<IXMLDOMNode> attrib;
        attributes->get_item(i, &attrib);

        // Get the name of the attribute
        CComBSTR name;
        attrib->get_nodeName(&name);

        CComVariant value;
        attrib->get_nodeValue(&value);
		if(name == "drawbars")
		{
			value.ChangeType(VT_I4);
			SetDrawbars(value.intVal);
		}
		else if(name == "amplitude")
		{
			value.ChangeType(VT_R8);
			m_amps = value.dblVal;
		}
		else if(name == "VibratoFrequency")
		{
			value.ChangeType(VT_R8);
			m_VibratoFrequency = value.dblVal;
		}
		else if(name == "VibratoMagnitude")
		{
			value.ChangeType(VT_R8);
			m_VibratoMagnitude = value.dblVal;
		}
		else if(name == "leslieFreq")
		{
			value.ChangeType(VT_R8);
			m_LeslieFrequency = value.dblVal;
			m_changeleslie = true;
		}
		else if(name == "leslieStartFreq")
		{
			value.ChangeType(VT_R8);
			m_LeslieStartFrequency = value.dblVal;
		}
		else if(name == "leslieEndFreq")
		{
			value.ChangeType(VT_R8);
			m_LeslieEndFrequency = value.dblVal;
		}
		else if(name == "leslieAttackTime")
		{
			value.ChangeType(VT_R8);
			m_LeslieAttackTime = value.dblVal;
		}
		else if(name == "leslieReleaseTime")
		{
			value.ChangeType(VT_R8);
			m_LeslieReleaseTime = value.dblVal;
		}
		else if(name == "LeslieRadius")
		{
			value.ChangeType(VT_R8);
			m_LeslieRadius = value.dblVal;
		}
	}
    if(m_LeslieFrequency == m_Leslie)
	{
		m_LeslieFrequency = m_LeslieEndFrequency;
		m_changeleslie = false;
	}
}
void COrganFactory::SetDrawbars(int drawbarNum)
{
	int value=0;
	double example=0.0;
	int simp1,simp2;
	double simp3;
	for(int i=0;i<9;i++)
	{
		simp1 = pow((double)10,(9-i));
		simp2 = pow((double)10,(8-i));
		value = drawbarNum%simp1;
		value = value/simp2;
		if(value!=0)
		{
			simp3 = pow((double)10,(((8-(double)value)*-3)/20));
			m_drawbars[i]=simp3;
		}
	}
}

